package ru.reaperoq.dto

import kotlinx.serialization.Serializable
import ru.reaperoq.database.Database
import ru.reaperoq.schema.Component
import ru.reaperoq.schema.Location

@Serializable
data class ComponentResponse(
    val id: Int,
    val name: String,
    val type: String,
    val specifications: String,
    val computerId: Int? = null,
    val purchaseDate: Long,
    val location: Location?,
    val serialNumber: String,
    val verifiedBy: UserResponse,
    val lastVerifiedAt: Long,
    val createdAt: Long,
    val createdBy: UserResponse
) {
    companion object {
        suspend fun from(component: Component, createdBy: UserResponse, verifiedBy: UserResponse): ComponentResponse {
            return ComponentResponse(
                component.id,
                component.name,
                component.type,
                component.specifications,
                component.computerId,
                component.purchasedAt,
                component.locationId?.let { Database.locationsService.read(it) },
                component.serialNumber!!,
                verifiedBy,
                component.lastVerifiedAt!!,
                component.createdAt,
                createdBy
            )
        }
    }
}
